--
-- Author: shunguo.chen
-- Date: 2019-05-16 16:02:00
--
-- LegendView   传奇武器view
--

local TabsBtn = require("app.widget.TabsBtn")
local LegendPanel = import(".LegendPanel")
local LegendStarPanel = import(".LegendStarPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.LegendScene

function ClassRef:init()
    self._ui.Button_close:addClickEventListener(function()
            executeFunc(self._onCloseView)
        end)

    -- TabsBtn
    local btns = {
        self._ui["Panel/Panel_tab/selected_btn_1"],
        self._ui["Panel/Panel_tab/selected_btn_2"],
        self._ui["Panel/Panel_tab/selected_btn_3"]
    }
    self._binds = {}
    local function onTabsBtn(index, btn)
        if self._curIdx then
            self._curIdx = index
            self:_switchPanel()
        else
            self._curIdx = index
        end
    end
    self._tabsBtn = TabsBtn.new(btns, onTabsBtn, nil, self._binds)

    self._subViewDict = {}
end

function ClassRef:remove()
    for k,panel in pairs(self._binds) do
        if panel.remove then
            panel:remove()
        end
    end
end

-- 检查是否触发了引导
function ClassRef:checkGuide()
    local funcId
    if self._curIdx == 1 then
        funcId = Constant.FUNC_TYPE_LEGEND
    elseif self._curIdx == 2 then
        funcId = Constant.FUNC_TYPE_LEGEND_STARMAP
    end

    if not funcId then return end

    local params = {
        scene = funcId,
        sceneSelf = self,
        getFunc = self.getGuideNodeFunc,
        nextFunc = self.checkGuide
    }
    EM:notify("checkGuide", params)
end

-- 获取引导node
function ClassRef:getGuideNodeFunc(id)
    if id == 511 or id == 512 or id == 513 then
        -- 点击基础界面里面的技能
        local panel = self._binds[1]
        if panel then
            return panel:getGuideNode(id)
        end
    elseif id == 521 then
        -- 点击切换到星图按钮
        return self._tabsBtn:getBtn(2)
    elseif id == 522  then
        -- 激活星图
        local panel = self._binds[2]
        if panel then
            return panel:getGuideNode(id)
        end
    end
end

function ClassRef:show(index)
    self.super.show(self)
    self:_switchPanel(index)
end

function ClassRef:onSwitchPanel(cb)
    self._onSwitchPanel = cb
end

function ClassRef:getPanel(index)
    return self._binds[index]
end

function ClassRef:_switchPanel(index)
    if index then
        self._curIdx = index
        self._tabsBtn:setSelected(index)
    end
    self:_createPanel(self._curIdx)
    executeFunc(self._onSwitchPanel, self._curIdx)

    if self._curIdx == 1 then
        self:refreshRedPoint(false)
    end

    self:checkGuide()
end

-- 红点
function ClassRef:refreshRedPoint(b)
    self._tabsBtn:getBtn(1):getChildByName("dot"):setVisible(self._curIdx ~= 1 and b)
end

function ClassRef:_createPanel(index)
    if not self._binds[index] then
        local panel
        if index == 1 or index == 3 then
            panel = LegendPanel.create(self._ui.draw)
        elseif index == 2 then
            panel = LegendStarPanel.create(self._ui.map)
        end
        if panel.onOperate then
            panel:onOperate(self._onOperate)
        end
        panel:setVisible(true)
        if index == 2 then
            self._binds[index] = panel
        else
            self._binds[1] = panel
            self._binds[3] = panel
        end
    end
end

-- 添加子界面，保持和主界面相同生命周期
function ClassRef:addSubView(view, name, zOrder)
    if not view then return end
    if not name then return end

    view:setName(name)
    self:addChild(view, zOrder or 1)
    self._subViewDict[name] = view
end

function ClassRef:getSubView(name)
    if not name then return end
    return self._subViewDict[name]
end

-- 再次在systemUI最上层时调用
function ClassRef:reOpen()
    -- if self._curIdx == 1 then
    --     self._binds[1]:reOpen()
    -- end

    executeFunc(self._onSwitchPanel, self._curIdx)

    -- 子界面
    for k,view in pairs(self._subViewDict) do
        if view:isVisible() and view.reOpen then
            view:reOpen()
        end
    end
end

return ClassRef
